//登录界面功能逻辑
var validateCodeCD = 60;//获取验证码冷却时间
var myCDcounter;//验证码冷却计时器
//var trueValidateCode = 0000;//真正的验证码
//console.log('test')
//注册按钮-跳转到注册界面
function toSignUp(){
    window.location.href = "/signUp";
}

//发送验证码按钮-发送验证码，启动计时器
function onSendValidateCodeClicked(){
    //异步请求验证码
    var phone = $("#phoneNumber").val();
    $.ajax({
        url:"/sms/sendCodeAgain",
        type:"POST",
        data:{phone:phone},

        success: function(data){
            if(data === "ok")
            {
                console.log("ask validateCode success");
                //启动计时器，修改按钮样式
                validateCodeCD = 60;
                $("#getValidateCode").attr("disabled","disabled");//禁用按钮
                myCDcounter = setInterval(countTime,1000);
            }
            else
            {
                //手机号码未注册
                showAlert("failAlertText");
                $("#failAlertText").text("手机号不存在");
            }

        }
    });

}

//计时器事件函数
function countTime(){
    validateCodeCD--;
    $("#getValidateCode").text(validateCodeCD+"秒后再次获取");
    if(validateCodeCD === 0){
        $("#getValidateCode").text("获取验证码");
        $("#getValidateCode").removeAttr("disabled");//复启用按钮
        clearInterval(myCDcounter);
    }
}
//手机号登录
var login_btn1 = document.querySelector("#login-phone")
login_btn1.addEventListener('click',login1);
function login1()
{
    let form1 = document.querySelector("#myForm");
    let formData1 = new FormData(form1);
    formData1.append("method","phone")
    $.ajax(
        {
            type: 'POST',
            url: 'http://localhost:8022/signIn',
            data: formData1,
            processData: false,
            contentType: false,
            success: function (data1)
            {
                if(data1 === "success"){
                    location.href='index';
                }
                else if(data1 === "invalid phone"){
                    //手机号码未注册
                    showAlert("failAlertText");
                    $("#failAlertText").text("手机号不存在");
                }
                else if (data1 === "invalid code"){
                    //验证码错误
                    showAlert("failAlertText");
                    $("#failAlertText").text("验证码错误");
                }
                else if (data1 === "over time"){
                    //验证码过期
                    showAlert("failAlertText");
                    $("#failAlertText").text("验证码过期");
                }
                else {
                    //未知错误
                }
            },
            error: function (errorMsg)
            {
                console.log("fail")
                console.log(errorMsg);
            },
        })
}

//账号密码登录
var login_btn = document.querySelector("#login-account")
login_btn.addEventListener('click',login);
function login()
{
    let form = document.querySelector("#myForm1");
    let formData = new FormData(form);
    formData.append("method","username");
    $.ajax(
        {
            type: 'POST',
            url: 'http://localhost:8022/signIn',
            data: formData,
            processData: false,
            contentType: false,
            success: function (data)
            {
                if(data === "success")
                {
                    console.log('登录成功');
                    location.href='index';//应跳往主页
                }
                else if(data === "invalid username")
                {
                    //用户名错误或用户不存在
                    showAlert("failAlertText");
                    $("#failAlertText").text("用户名错误或用户不存在");
                }
                else if(data === "wrong password")
                {
                    //密码错误
                    showAlert("failAlertText");
                    $("#failAlertText").text("密码错误");
                }
                else
                {
                    //未知错误
                }
            },
            error: function (errorMsg)
            {
                console.log("fail")
                console.log(errorMsg);
            },
        })
}

//显示信息框
function showAlert(alertId){
    $("#"+alertId).removeClass("invisible");
}

//隐藏信息框
function hideAlert(alertId){
    $("#"+alertId).addClass("invisible");
}